package com.mongdb.mongdb;

import com.mongodb.BasicDBObject;
import com.mongodb.DBObject;
import com.sun.org.apache.bcel.internal.generic.NEW;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.BasicQuery;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.AsyncResult;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.sql.Date;
import java.sql.Timestamp;
import java.util.List;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;
import java.util.concurrent.FutureTask;
import java.util.stream.IntStream;

@RestController
@RequestMapping(value = "/mong")

public class MongController {
	public static final Logger log = LoggerFactory.getLogger(MongController.class);

	public static final String COLLECTION = "CustomDataRow";

	@Autowired
	private MongoTemplate mongoTemplate;

	@Autowired
	private AsyncTask asyncTask;
	@GetMapping
	public List<User> getMongObjectByPage(){
//		DBObject dbObj = new BasicDBObject();
//		BasicQuery query = new BasicQuery(dbObj);

		Query query=new Query();

		//精确查询
		//Query: { "name" : "stram5" }, Fields: { }, Sort: { }
		Criteria isCritera=Criteria.where("name").is("stram5");

		//模糊查询
		//db.getCollection('CustomDataRow').find({"name" : { "$regex" : "菜菜", "$options" : "" }})
		//Criteria criteria = Criteria.where("name").regex("菜菜");
		//query.addCriteria(criteria);

		Criteria incriteria = Criteria.where("_id").in("2");
		//query.addCriteria(incriteria);

		//or 关系 需要在[]括号里面，在外面的为与关系
		//Query: { "$or" : [{ "name" : { "$regex" : "菜菜", "$options" : "" } }, { "_id" : { "$in" : ["2"] } }] }, Fields: { }, Sort: { }
//		Criteria orCriteria = new Criteria();
//		Criteria[] arrayCriteria = new Criteria[2];
//		arrayCriteria[0] = criteria;
//		arrayCriteria[1] = incriteria;
//		orCriteria.orOperator(arrayCriteria);

		//query.addCriteria(isCritera);
		//排序Query: { }, Fields: { }, Sort: { "createTime" : -1 }
		query.with(new Sort(Sort.Direction.DESC, "createTime"));
		List<User> datas = mongoTemplate.find(query, User.class,COLLECTION);
		return datas;
	}


	//需要在启动类添加扫描@EnableAsync，否则方法任然为同步执行
	//不能在本类中添加@async方法去调用，那样调用任然为同步
	//本方法添加@async，本类调用方法也添加@async调用为同步
	//调用方法需要加到另外一个bean管理的类中，然后通过bean调用的方式才会是异步执行的通过观察线程的名字可以知道
	@GetMapping(value = "/insert")
	public String SaveMonObject(){

		long start = System.currentTimeMillis();
		//IntStream.range(4,6).forEach(i-> {

			User user = new User(new Timestamp(System.currentTimeMillis()));
			user.setId(4+"");
			user.setName("stram"+4);
			user.setSex("男");
			//异步插入
			Future<String> s=asyncTask.asyncInsert(user);

			User user1 = new User(new Timestamp(System.currentTimeMillis()));
			user1.setId(5+"");
			user1.setName("stram"+5);
			user1.setSex("男");

			Future<String> s1=asyncTask.asyncInsert(user1);


			try {
				System.out.println(s.get());
				System.out.println(s1.get());
			} catch (InterruptedException e) {
				e.printStackTrace();
			} catch (ExecutionException e) {
				e.printStackTrace();
			}
		//});
		long end = System.currentTimeMillis();
		String times = "任务全部完成，总耗时："+(end-start)+"毫秒";
		log.info("主线程"+times);
		return "111";
	}





}
